home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 003 / xlisp / object.lsp < prev    next >
Lisp/Scheme  |  1995-03-17  |  2KB  |  68 lines

  1. ; This is an example using the object-oriented programming support in
  2. ; XLISP.  The example involves defining a class of objects representing
  3. ; dictionaries.  Each instance of this class will be a dictionary in
  4. ; which names and values can be stored.  There will also be a facility
  5. ; for finding the values associated with names after they have been
  6. ; stored.
  7.  
  8. ; Create the 'Dictionary' class.
  9.  
  10. (setq Dictionary (Class 'new))
  11.  
  12. ; Establish the instance variables for the new class.
  13. ; The variable 'entries' will point to an association list representing the
  14. ; entries in the dictionary instance.
  15.  
  16. (Dictionary 'ivars '(entries))
  17.  
  18. ; Setup the method for the 'isnew' initialization message.
  19. ; This message will be send whenever a new instance of the 'Dictionary'
  20. ; class is created.  Its purpose is to allow the new instance to be
  21. ; initialized before any other messages are sent to it.  It sets the value
  22. ; of 'entries' to nil to indicate that the dictionary is empty.
  23.  
  24. (Dictionary 'answer 'isnew '()
  25.         '((setq entries nil)
  26.           self))
  27.  
  28. ; Define the message 'add' to make a new entry in the dictionary.  This
  29. ; message takes two arguments.  The argument 'name' specifies the name
  30. ; of the new entry; the argument 'value' specifies the value to be
  31. ; associated with that name.
  32.  
  33. (Dictionary 'answer 'add '(name value)
  34.         '((setq entries
  35.                 (cons (cons name value) entries))
  36.           value))
  37.  
  38. ; Create an instance of the 'Dictionary' class.  This instance is an empty
  39. ; dictionary to which words may be added.
  40.  
  41. (setq d (Dictionary 'new))
  42.  
  43. ; Add some entries to the new dictionary.
  44.  
  45. (d 'add 'mozart 'composer)
  46. (d 'add 'winston 'computer-scientist)
  47.  
  48. ; Define a message to find entries in a dictionary.  This message takes
  49. ; one argument 'name' which specifies the name of the entry for which to
  50. ; search.  It returns the value associated with the entry if one is
  51. ; present in the dictionary.  Otherwise, it returns nil.
  52.  
  53. (Dictionary 'answer 'find '(name &aux entry)
  54.         '((cond ((setq entry (assoc name entries))
  55.           (cdr entry))
  56.          (t
  57.           nil))))
  58.  
  59. ; Try to find some entries in the dictionary we created.
  60.  
  61. (d 'find 'mozart)
  62. (d 'find 'winston)
  63. (d 'find 'bozo)
  64.  
  65. ; The names 'mozart' and 'winston' are found in the dictionary so their
  66. ; values 'composer' and 'computer-scientist' are returned.  The name 'bozo'
  67. ; is not found so nil is returned in this case.
  68.